package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.config;

import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizPerformanceConfig;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 换电绩效配置的业务对象
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/18
 */
@Data
public class PowerChangePerformanceConfig implements BizPerformanceConfig {
    /**
     * 区域id
     */
    private Integer accountId;

    /**
     * 常规方案-基础绩效
     */
    private BasicPerformanceGroup conventionBasic;
    /**
     * 常规方案-产生订单时段绩效
     */
    //private IntervalBasedGroup intervalBased;
    /**
     * 常规方案-产生订单时效绩效
     */
    //private ValidityBasedGroup validityBased;
    /**
     * 产生订单时段与时效组合绩效
     */
    private IntervalAndValidityBasedGroup intervalAndValidityBased;
    /**
     * 常规方案-低电优先换电绩效
     */
    private LowPowerBasedGroup lowPowerBased;
    /**
     * 常规方案-换电后电量绩效
     */
    private PowerAfterChangeBasedGroup powerAfterChangeBased;

    /**
     * 站点优先方案-基础绩效
     */
    private BasicPerformanceGroup stationBasic;
    /**
     * 站点优先方案-站点优先级绩效
     */
    private StationPriorityBasedGroup stationPriorityBased;

    public static PowerChangePerformanceConfig fromRaw(List<PerformanceRuleGroup> groups, Integer accountId) {
        PowerChangePerformanceConfig config = new PowerChangePerformanceConfig();
        config.setAccountId(accountId);
        for (PerformanceRuleGroup group : groups) {
            switch (group.getRuleType()) {
                case BASIC:
                    if (group.getBizType() == PerformanceBizType.POWER_CHANGE_CONVENTION) {
                        config.setConventionBasic(new BasicPerformanceGroup());
                        config.getConventionBasic().initFromRaw(group);
                    } else {
                        config.setStationBasic(new BasicPerformanceGroup());
                        config.getStationBasic().initFromRaw(group);
                    }
                    break;
                case INTERVAL_AND_VALIDITY:
                    config.setIntervalAndValidityBased(new IntervalAndValidityBasedGroup());
                    config.getIntervalAndValidityBased().initFromRaw(group);
                    break;
                case LOW_POWER:
                    config.setLowPowerBased(new LowPowerBasedGroup());
                    config.getLowPowerBased().initFromRaw(group);
                    break;
                case POWER_AFTER_CHANGE:
                    config.setPowerAfterChangeBased(new PowerAfterChangeBasedGroup());
                    config.getPowerAfterChangeBased().initFromRaw(group);
                    break;
                case STATION_PRIORITY:
                    config.setStationPriorityBased(new StationPriorityBasedGroup());
                    config.getStationPriorityBased().initFromRaw(group);
                    break;
            }
        }
        return config;
    }


    @Override
    public List<PerformanceRuleGroup> toRaw() {
        BizRuleGroup[] conventionGroups = new BizRuleGroup[]{
                conventionBasic,
                intervalAndValidityBased,
                lowPowerBased,
                powerAfterChangeBased,
        };

        BizRuleGroup[] stationGroups = new BizRuleGroup[]{
                stationBasic,
                stationPriorityBased,
        };

        List<PerformanceRuleGroup> groups = new ArrayList<>(7);
        for (BizRuleGroup group : conventionGroups) {
            if (group != null) {
                groups.add(group.toRaw(PerformanceBizType.POWER_CHANGE_CONVENTION, accountId));
            }
        }
        for (BizRuleGroup group : stationGroups) {
            if (group != null) {
                groups.add(group.toRaw(PerformanceBizType.POWER_CHANGE_STATION, accountId));
            }
        }
        return groups;
    }

    @Override
    public void initDefaultIfNull() {
        if (conventionBasic == null) {
            conventionBasic = new BasicPerformanceGroup();
            conventionBasic.initDefault();
        }
        if (intervalAndValidityBased == null) {
            intervalAndValidityBased = new IntervalAndValidityBasedGroup();
            intervalAndValidityBased.initDefault();
        }
        if (lowPowerBased == null) {
            lowPowerBased = new LowPowerBasedGroup();
            lowPowerBased.initDefault();
        }
        if (powerAfterChangeBased == null) {
            powerAfterChangeBased = new PowerAfterChangeBasedGroup();
            powerAfterChangeBased.initDefault();
        }
        if (stationBasic == null) {
            stationBasic = new BasicPerformanceGroup();
            stationBasic.initDefault();
        }
        if (stationPriorityBased == null) {
            stationPriorityBased = new StationPriorityBasedGroup();
            stationPriorityBased.initDefault();
        }
    }
}
